<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    pageContext.setAttribute("PAGETITLE", "部门编辑");
%>
<%@ include file="/pages/include/header.jsp" %>

<script type="text/javascript">
<!--
function cmd(command, params) {
    switch (command) {
       case "add":
           var url = ctx + "/pages/system/deptEdit.jsp?parentId=" + $E("form.deptId").value;
           window.showModalDialog(url, null, "dialogWidth=700px; dialogHeight=600px; status=0;");
           refresh({deptId: $E("form.deptId").value});
           break;
       case "edit":
           var url = ctx + "/action/dept.do?method=edit&deptId=" + params.deptId;
           window.showModalDialog(url, null, "dialogWidth=700px; dialogHeight=600px; status=0;");
           refresh({deptId: $E("form.deptId").value});
           break;
       case "delete":
           if (window.confirm("确定删除?")) {
               var url = ctx + "/action/dept.do";
               $.post(url, {method: "delete", deptId: params.deptId}, function (returnString) {
                   var i = parseInt(returnString);
                   if (0 < i) {
                       refresh({deptId: $E("form.deptId").value});
                   } else {
                       $("#action_result").html("操作失败");
                       $("#action_result").addClass("action_result_0");
                       $C.showTips({selector: "#action_result", position: $C.P_LEFT, hide: true});
                   }
               });
           }
           break;
    }
}

var refresh = function (params) {
    $E("deptId").value = params.deptId;
    $E("refreshForm").submit();
};

$(document).ready(function () {
    var ar = $("#action_result");
    var i = parseInt(ar.html());
    if (0 < i) {
        ar.addClass("action_result_1");
        ar.html("操作成功");
    } else if (0 === i) {
        ar.addClass("action_result_0");
        ar.html("操作失败");
    }
    if (ar.html()) {
        $C.showTips({selector: "#action_result", position: $C.P_LEFT, hide: true});
    } else {
        $("#action_result").hide();
    }
});
//-->
</script>

<sql:query var="userList" dataSource="${dataSource}">
SELECT USERID, USERNAME FROM CT_USER;
</sql:query>

<sql:query var="deptList" dataSource="${dataSource}">
SELECT D.*, (CASE WHEN D.STATUS = '1' THEN '有效' ELSE '无效' END) STATUS2
FROM CT_DEPARTMENT D WHERE D.PARENTID = '${form.map.form.deptId}'
ORDER BY D.ORDERNO
</sql:query>

<body>

    <div class="page_header">${PAGETITLE}</div>
    
    <div class="page_buttons">
        <div id="action_result" class="action_result">${requestScope.ACTION_RESULT}</div>
        <input type="button" onclick="$('#form').submit()"
            value="${empty form.map.form.deptId ? '保存' : '更新'}" />
    </div>

    <html:form action="/action/dept" styleId="form">
        <input type="hidden" id="method" name="method"
            value="${empty form.map.form.deptId ? 'insert' : 'update'}" />
    
        <html:hidden property="form.deptId" styleId="form.deptId" />
        <html:hidden property="form.parentId"
            value="${empty form.map.form.deptId ? param.parentId : form.map.form.parentId}" />
        
        <table width="100%" cellpadding="0" cellspacing="0" align="center">
            <tr style="position: absolute; left: -5000px; top: -5000px;">
                <td width="20%"></td>
                <td width="80%"></td>
            </tr>
            <tr>
                <td class="label">部门编号：</td>
                <td class="value">
                    <html:text property="form.deptCode" style="width: 98%;" />
                </td>
            </tr>
            <tr>
                <td class="label">部门名称：</td>
                <td class="value">
                    <html:text property="form.deptName" style="width: 98%;" />
                </td>
            </tr>
            <tr>
                <td class="label">部门简称：</td>
                <td class="value">
                    <html:text property="form.deptAbbr" style="width: 98%;" />
                </td>
            </tr>
            <tr>
                <td class="label">部门领导：</td>
                <td class="value">
                    <html:select property="form.deptChief">
                        <html:option value=""></html:option>
                        <c:forEach var="item" items="${userList.rows}">
                        <html:option value="${item.USERID}">${item.USERNAME}</html:option>
                        </c:forEach>
                    </html:select>
                </td>
            </tr>
            <tr>
                <td class="label">状态：</td>
                <td class="value">
                    <html:select property="form.status">
                        <html:option value=""></html:option>
                        <html:option value="1">有效</html:option>
                        <html:option value="0">无效</html:option>
                    </html:select>
                </td>
            </tr>
            <tr>
                <td class="label">排序号：</td>
                <td class="value">
                    <html:text property="form.orderNo" styleClass="number" maxlength="7" />
                </td>
            </tr>
            <tr>
                <td class="label">备注：</td>
                <td class="textarea">
                    <html:textarea property="form.remark" style="width: 98%;" rows="5" />
                </td>
            </tr>
        </table>
    </html:form>
    
    <c:if test="${not empty form.map.form.deptId}">
    <div class="deptChildren">
        <div>子部门</div>
        <input type="button" value="增加" onclick="cmd('add')" />
    </div>
    <table cellpadding="0" cellspacing="0" align="center" class="table_list">
        <thead>
            <tr>
                <td width="45">序号</td>
                <td>部门编号</td>
                <td>部门名称</td>
                <td>部门简称</td>
                <td>部门领导</td>
                <td width="65">是否有效</td>
                <td width="60">排序号</td>
                <td width="90">操作</td>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="item" items="${deptList.rows}" varStatus="vs">
            <tr>
                <td align="center">${vs.index + 1}</td>
                <td align="left">&nbsp;${item.DEPTCODE}</td>
                <td align="left">&nbsp;${item.DEPTNAME}</td>
                <td align="left">&nbsp;${item.DEPTABBR}</td>
                <td align="left">&nbsp;${item.USERNAME}</td>
                <td align="center">&nbsp;${item.STATUS2}</td>
                <td align="right">&nbsp;${item.ORDERNO}</td>
                <td align="center">
                    <a href="javascript:cmd('edit', {deptId: '${item.DEPTID}'})">编辑</a>
                    <a href="javascript:cmd('delete', {deptId: '${item.DEPTID}'})">删除</a>
                </td>
            </tr>
            </c:forEach>
        </tbody>
    </table>
    
    <form action="${ctx}/action/dept.do" method="post" id="refreshForm" style="display: none">
        <input type="hidden" name="method" value="edit" />
        <input type="hidden" id="deptId" name="deptId" />
    </form>
    </c:if>
    
    <div class="page_footer"></div>

</body>

<%@ include file="/pages/include/footer.jsp" %>
